﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YunSpace.Application.SystemManage;
using YunSpace.Core;
using YunSpace.Data;
using YunSpace.Domain.Entity.SiteMgr;
using YunSpace.Domain.Entity.SystemManage;
using YunSpace.Domain.IRepository.SiteMgr;
using YunSpace.Domain.IRepository.SystemManage;
using YunSpace.Repository.SiteMgr;
using YunSpace.Repository.SystemManage;
using YunSpace.ThirdService;

namespace YunSpace.Application.PushHandle
{
    public class PushHandleManage
    {
        MailHelper mail = new MailHelper();
        RongLianSmsManger sms = new RongLianSmsManger();
        private IUserRepository userService = new UserRepository();
        private ISiteClientsProjectByContactRepository contactService = new SiteClientsProjectByContactRepository();
        private ISiteClientsContactMappingRepository contactMappingService = new SiteClientsContactMappingRepository();
        private ISiteClientsProjectByContactRepository contactRepository = new SiteClientsProjectByContactRepository();
        private ISiteClientsRepository clientRepository = new SiteClientsRepository();

        /// <summary>
        /// 分单邮件和短信处理机制
        /// </summary>
        public void ShareSingle(string F_FlowUserId, SiteClientsProjectByDemandEntity demandEntity) {
            ///获取用户权限
            UserEntity user= userService.FindEntity(F_FlowUserId);
            OrganizeApp organize = new OrganizeApp();
            OrganizeEntity organizeEntity = organize.GetForm(user.F_DepartmentId);
            OrganizeEntity F_GroupEntity= organize.GetForm(user.F_GroupId);

            //获取部门负责人信息
            UserEntity mbUser=null;
            if (organizeEntity != null) {
                mbUser = userService.FindEntity(organizeEntity.F_ManagerId);
            }
            //获取部门小组负责人信息
            UserEntity groupUser=null;
            if(F_GroupEntity!=null)
            {
                groupUser = userService.FindEntity(F_GroupEntity.F_ManagerId);
            }             

            //需求联系人
            SiteClientsContactMappingEntity contactMapping = GetContactMapping(demandEntity.F_Id);
            SiteClientsProjectByContactEntity contactEntity= contactRepository.FindEntity(contactMapping.F_ContactId);
            //客户名称
            SiteClientsEntity client = clientRepository.FindEntity(demandEntity.F_ClientId);

            string[] data = new string[0];
            List<string> datas = new List<string>();
            datas.Add(user.F_RealName);
            datas.Add(contactEntity.F_Contact);
            datas.Add(contactEntity.F_Phone);
            datas.Add(client.F_ClientName);
            datas.Add(demandEntity.F_ActivityTheme);
            data = datas.ToArray();

            string phones = "";
            if (user != null) {
                phones += user.F_MobilePhone;
            }
            if (mbUser != null) {
                phones +=","+ mbUser.F_MobilePhone;
            }
            if (groupUser != null) {
                phones += "," + groupUser.F_MobilePhone;
            }
            sms.SendGiveOrderInfo(phones, data);           
        }

        public SiteClientsContactMappingEntity GetContactMapping(string demandId)
        {
            SiteClientsContactMappingEntity mapping = new SiteClientsContactMappingEntity();
            using (var db = new RepositoryBase())
            {
                string sql = @"select * from Site_Clients_ContactMapping where F_DemandId='" + demandId + "'";
                mapping = db.FindList<SiteClientsContactMappingEntity>(sql).FirstOrDefault();
            }
            return mapping;
        }
    }
}
